/*
 * Copyright © 2018 iFund. All rights reserved.
 */

package com.climber.android.commonres.helper

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.util.Pair
import android.util.TypedValue
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import android.view.View
import android.widget.TextView
import androidx.fragment.app.FragmentManager
import com.climber.android.commonres.R
import com.climber.android.commonres.ui.dialog.DoubleOptionsPickerDialog
import com.climber.android.commonres.ui.dialog.SingleOptionsPickerDialog
import com.climber.android.commonres.ui.dialog.TripleOptionsPickerDialog
import com.climber.android.commonsdk.util.copytext.OnSelectListener
import com.climber.android.commonsdk.util.copytext.SelectableTextHelper
import io.ganguo.library.mvp.util.ToastHelper
import io.ganguo.library.mvp.util.UIUtils

/**
 * @author Mikes
 * @date 2018-5-15
 */
fun View.showSingleOptionDialog(manager: FragmentManager,
                                    singleOptions: Array<String>,
                                    completion: ((Int) -> Unit)) {
    val dialogFragment = SingleOptionsPickerDialog.newInstance(singleOptions,
        context.getString(R.string.common_cancel),
        context.getString(R.string.common_ok),
        false,
        true,
        true)
    dialogFragment.show(manager, "single_options")

    dialogFragment.setWheelDialogListener(object : SingleOptionsPickerDialog.OnWheelDialogListener {
        override fun onClickLeft(value: String?, position: Int) {
            //cancel
            dialogFragment.dismiss()
        }

        override fun onClickRight(value: String?, position: Int) {
            dialogFragment.dismiss()
            completion(position)
        }

        override fun onValueChanged(value: String?) {
        }
    })
}

fun View.showDoubleOptionDialog(manager: FragmentManager,
                                    leftOptions: Array<String>,
                                    rightOptions: Array<String>,
                                    completion: ((leftPos : Int, rightPos : Int) -> Unit)) {
    val dialogFragment = DoubleOptionsPickerDialog.newInstance(
        leftOptions,
        rightOptions,
        context.getString(R.string.common_cancel),
        context.getString(R.string.common_ok),
        false,
        true,
        true)
    dialogFragment.show(manager, "double_options")

    dialogFragment.setWheelDialogListener(object : DoubleOptionsPickerDialog.OnWheelDialogListener {
        override fun onLeftWheelValueChanged(value: String) {
        }

        override fun onRightWheelValueChanged(value: String) {
        }

        override fun onClickLeft(valuePair: Pair<String, String>, posPair: Pair<Int, Int>) {
            dialogFragment.dismiss()
        }

        override fun onClickRight(valuePair: Pair<String, String>, posPair: Pair<Int, Int>) {
            dialogFragment.dismiss()
            completion(posPair.first, posPair.second)
        }
    })
}

fun View.showTripleOptionDialog(manager: FragmentManager,
                                    leftOptions: Array<String>,
                                    middleOptions: Array<String>,
                                    rightOptions: Array<String>,
                                    completion: ((leftPos : Int,middlePos : Int, rightPos : Int) -> Unit)) {
    val dialogFragment = TripleOptionsPickerDialog.newInstance(
        leftOptions,
        middleOptions,
        rightOptions,
        context.getString(R.string.common_cancel),
        context.getString(R.string.common_ok),
        false,
        true,
        true)
    dialogFragment.show(manager, "triple_options")

    dialogFragment.setWheelDialogListener(object : TripleOptionsPickerDialog.OnWheelDialogListener {
        override fun onClickLeft(
            valuePair: Triple<String, String, String>,
            posPair: Triple<Int, Int, Int>
        ) {
            dialogFragment.dismiss()
        }

        override fun onClickRight(
            valuePair: Triple<String, String, String>,
            posPair: Triple<Int, Int, Int>
        ) {
            dialogFragment.dismiss()

            completion(posPair.first, posPair.second, posPair.third)
        }

        override fun onValueChanged(valuePair: Triple<String, String, String>) {
        }

    })
}

fun TextView.showCountryCodeSpinner(manager: FragmentManager, completion: ((countryCode : String?) -> Unit)) {
    val countryCodeAndNames = mutableListOf<String>()
    val countryCodeResource = arrayListOf("+86","+852","+853","+886")

    countryCodeAndNames.add(resources.getString(R.string.code_86) + " +86")
    countryCodeAndNames.add(resources.getString(R.string.code_852) + " +852")
    countryCodeAndNames.add(resources.getString(R.string.code_853) + " +853")
    countryCodeAndNames.add(resources.getString(R.string.code_886) + " +886")

    val dialogFragment = SingleOptionsPickerDialog.newInstance(countryCodeAndNames.toTypedArray(),context.getString(R.string.common_cancel), context.getString(R.string.common_ok),
            false, true, true)
    dialogFragment.show(manager, "country_code")

    dialogFragment.setWheelDialogListener(object : SingleOptionsPickerDialog.OnWheelDialogListener {
        override fun onClickLeft(value: String?, position: Int) {
            //cancel
            dialogFragment.dismiss()
            completion(null)
        }

        override fun onClickRight(value: String?, position: Int) {
            dialogFragment.dismiss()
            text = countryCodeResource[position]
            completion(countryCodeResource[position])
        }

        override fun onValueChanged(value: String?) {
        }
    })
}

fun TextView.showErrorTips(textResId: Int){
    this.visibility = View.VISIBLE
    this.setText(textResId)
    this.setTextColor(resources.getColor(R.color.txt_invalid_color))
    this.setBackgroundResource(R.drawable.shape_error_tip_bg)
}

fun TextView.showErrorTips(textStr: String){
    this.visibility = View.VISIBLE
    this.text = textStr
    this.setTextColor(resources.getColor(R.color.txt_invalid_color))
    this.setBackgroundResource(R.drawable.shape_error_tip_bg)
}

fun TextView.showWarningTips(textResId: Int){
    this.visibility = View.VISIBLE
    this.setText(textResId)
    this.setTextColor(resources.getColor(R.color.txt_valid_color))
    this.setBackgroundResource(R.drawable.shape_warning_tip_bg)
}

fun TextView.showWarningTips(textStr: String){
    this.visibility = View.VISIBLE
    this.text = textStr
    this.setTextColor(resources.getColor(R.color.txt_valid_color))
    this.setBackgroundResource(R.drawable.shape_warning_tip_bg)
}

fun TextView.clearError() {
    this.visibility = View.GONE
    this.text = ""
}

fun TextView.highStrength(textResId: Int) {
    this.setText(textResId)
    this.setTextColor(Color.parseColor("#12AE39"))
}

fun TextView.highStrength(textStr: String) {
    this.text = textStr
    this.setTextColor(Color.parseColor("#12AE39"))
}

fun TextView.setDynamicNotificationTypeBackgroundShape(argb : Int){
    val shapeBackground = GradientDrawable()
    shapeBackground.shape = GradientDrawable.RECTANGLE
    shapeBackground.cornerRadius = 7F
    shapeBackground.setColor(argb)
    background = shapeBackground
}

fun TextView.changeLeftDrawableIcon(context: Context, @DrawableRes iconResId: Int?, iconSize : Int){
    if (iconResId != null) {
        val drawable = ContextCompat.getDrawable(context, iconResId)
        drawable?.setBounds(0, 0, UIUtils.dip2Px(iconSize), UIUtils.dip2Px(iconSize))
        this.setCompoundDrawables(drawable,null , null, null)
    } else {
        this.setCompoundDrawables(null,null , null, null)
    }
}

fun TextView.changeRightDrawableIcon(context: Context, @DrawableRes iconResId: Int?, iconSize : Int){
    if (iconResId != null) {
        val drawable = ContextCompat.getDrawable(context, iconResId)
        drawable?.setBounds(0, 0, UIUtils.dip2Px(iconSize), UIUtils.dip2Px(iconSize))
        this.setCompoundDrawables(null,null , drawable, null)
    } else {
        this.setCompoundDrawables(null,null , null, null)
    }
}

fun TextView.changeTopDrawableIcon(context: Context, @DrawableRes iconResId: Int?, iconSize : Int){
    if (iconResId != null) {
        val drawable = ContextCompat.getDrawable(context, iconResId)
        drawable?.setBounds(0, 0, iconSize, iconSize)
        this.setCompoundDrawables(null,drawable , null, null)
    } else {
        this.setCompoundDrawables(null,null , null, null)
    }
}

fun View.setDynamicRspStatusBackgroundShape(argb : Int){
    val shapeBackground = GradientDrawable()
    shapeBackground.shape = GradientDrawable.RECTANGLE
    shapeBackground.cornerRadius = UIUtils.dip2Px(5).toFloat()
    shapeBackground.setColor(argb)
    background = shapeBackground
}

fun hideMultiViews(vararg views: View){
    for(view in views){
        view.visibility = View.GONE
    }
}

fun showMultiViews(vararg views: View){
    for(view in views){
        view.visibility = View.VISIBLE
    }
}

/**
 * @param internalTime 两次点击按钮之间的点击间隔不能少于600毫秒
 */
fun android.view.View.onClick(internalTime : Int = 600, handler : (android.view.View)-> Unit){
    var lastClickTime = 0L
    setOnClickListener { v ->
        val curClickTime = System.currentTimeMillis()
        if ((curClickTime - lastClickTime) >= internalTime) {
            lastClickTime = curClickTime
            handler(v)
        }
    }
}

/**
 * 双击判断
 * @param internalTime 600毫秒以内的2次点击认为是双击
 */
fun android.view.View.onDoubleClick(internalTime : Int = 600, handler : (android.view.View)-> Unit){
    var lastClickTime = 0L
    var clickCount = 0
    setOnClickListener { v ->
        clickCount = if (System.currentTimeMillis() - lastClickTime < internalTime) ++clickCount else 1
        lastClickTime = System.currentTimeMillis()
        if (clickCount == 2) {
            clickCount = 0
            handler(v)
        }
    }
}

// 设置用户等级背景
fun TextView.changeUserLevelBackground(bgColor : Int){
    val shadowRect = GradientDrawable()
    shadowRect.shape = GradientDrawable.RECTANGLE
    shadowRect.cornerRadius = UIUtils.dip2Px(20).toFloat()
    shadowRect.setColor(bgColor)

    this.background = shadowRect
    this.setTextColor(Color.WHITE)
    this.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10F)
    this.setPadding(UIUtils.dip2Px(7), UIUtils.dip2Px(4), UIUtils.dip2Px(7), UIUtils.dip2Px(4))
}

fun TextView.setSelectableHelper(){
    SelectableTextHelper.Builder(this)
            .setSelectedColor(UIUtils.getColor(R.color.copy_select_color))
            .setCursorHandleSizeInDp(20F)
            .setCursorHandleColor(UIUtils.getColor(R.color.copy_cursor_color))
            .build()
            .setSelectListener(object : OnSelectListener() {
                override fun onTextSelected(content: CharSequence) {
                    ToastHelper.toastMessage(R.string.copy_succeed)
                }
            })
}



